package DesginFotmat;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class GuardedSuspend {

    public static void main(String[] args) {
        GuardedObject guardedObject = new GuardedObject();
        new Thread(()->{
            log.debug("等待结果");
            List<String>  list =(List<String>) guardedObject.getResponse();
            log.debug("结果大小：{}",list.size());
        },"t1").start();

        new Thread(()->{
            log.debug("执行下载");
            try {
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            List<String> list = new ArrayList<>();
            list.add("你");
            list.add("好");
            guardedObject.complete(list);

        },"t2").start();
    }
}



class GuardedObject {
    //结果
    private Object response;


    //获取结果
    public Object getResponse() {
        synchronized (this) {
            //没有结果
            while (response == null) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            return response;
        }
    }

    //产生结果
    public void complete(Object response) {
        synchronized (this) {
            //给结果成员变量赋值
            this.response = response;
            this.notifyAll();
        }
    }

}
